package com.example.studentalarm.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;

/**
 * 声音配置工具类
 */
public class SoundConfigUtils{

    private static SoundConfigUtils soundConfigUtils;
    private Context context;

    public static SoundConfigUtils getInstance(Context context) {
        if (soundConfigUtils == null) {
            soundConfigUtils = new SoundConfigUtils(context);
        }
        return soundConfigUtils;
    }

    private SoundConfigUtils(Context context) {
        super();
        this.context = context;
    }


    public enum RingerMode{
        RINGER_MODE_NORMAL, // 响铃+震动
        RINGER_MODE_SILENT, // 响铃不震动
        RINGER_MODE_VIBRATE // 只振动
    }

    /**
     * 播放声音
     */
    @SuppressWarnings("deprecation")
    public void startSound(final RingerMode ringerMode) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);   //震动服务
        long[] pattern = {100, 400, 100, 400};          //100表示等待时间,400表示震动时间
        //(1)vibrator.vibrate(pattern,-1); 表示不重复,只震动一个循环;
        vibrator.vibrate(pattern,1);//表示从下标为1的开始重复震动 ;
        //(3)vibrator.vibrate(1000),表示震动多少秒
        switch (ringerMode) {    //获取系统设置模式
            //静音
            case RINGER_MODE_SILENT:
                break;
            //常规模式 两种情况:1.响铃不震动 2.响铃+震动
            case RINGER_MODE_NORMAL:
                new Thread() {
                    @Override
                    public void run() {
                        //soundPool播放
                        initSoundPool();
                        try {
                            Thread.sleep(1000);   //线程休眠1秒,为系统加载音乐资源文件
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        playSound( -1);
                    }
                }.start();
                vibrator.vibrate(pattern, 1);
                break;
            //震动
            case RINGER_MODE_VIBRATE:
                vibrator.vibrate(pattern, 1);
                break;
            default:
                break;
        }
    }

    /**
     * soundPool提示音效
     * (1)初始化播放音频的组件
     * (2)初始化播放音效列表(HashMap=(ID,Sp.load(...))
     * (3)获取系统声音设置(设置左右音道)
     * (4)play
     */
    private SoundPool sp;
    private Map<Integer, Integer> sources = null;
    private int streamId = 0;

    /**
     * 初始化音频
     */
    @SuppressLint("UseSparseArrays")
    private void initSoundPool() {
        sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);  // (最多同时播放的音乐,音频的类型,品质)
        sources = new HashMap<Integer, Integer>();
        try {
            sources.put(0, sp.load(context.getAssets().openFd("enter.mp3"), 1));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 播放音效
     *
     * @param loop
     */
    private void playSound(int loop) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);   //系统声音管理类
        float currentSound = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);                 //最大音量
        float maxSound = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);                  //当前音量
        streamId = sp.play(sources.get(0), currentSound / maxSound, currentSound / maxSound, 1, loop, 1.0f);                                            //加载资源id(sp.load(...)的返回值,0表示加载失败)
        // 左声道,右声道,优先级,循环次数(0,播放一次,-1无限次,其他播放loop+1)
        // 播放速率(0.5f-2.0f,1.0f表示正常,2.0f表示两倍速率)
        // 返回值为streamId可作为之后的pause,stop的参数
    }

    /**
     * 关闭声音
     */
    public void stopSound() {
        if (sp != null) {
            if (streamId != 0) {
                sp.stop(streamId);
            }
        }
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);   //震动服务
        vibrator.cancel();
    }

}